﻿#include "Tools.h"
#include <time.h> 
#include <chrono>
#ifdef __linux__
#include <unistd.h>	//linux
#include <sys/stat.h> 
#include <sys/types.h> 
#elif _WIN32
#include <io.h>		//windows
#include <Windows.h>
#include <direct.h>
#endif
#include<fstream>

#define DataTime_Format "%Y-%m-%d %H:%M:%S" // # 时间格式化
#define Data_Format "%Y-%m-%d"

time_t Tools::GetTimeStamp(tm pDateTime)
{
	time_t t_ = mktime(&pDateTime); //已经减了8个时区
	return t_; //秒时间
}

tm Tools::GetDateTimeFrmStamp(time_t pStamp)
{
	struct tm* ptm = localtime(&pStamp);
	return *ptm;
}

std::string Tools::DateTimeStamp2Str(time_t stampTime)
{
	//time_t tick = (time_t)stampTime;
	char s[100];
	struct tm p;
	p = *localtime(&stampTime);
	strftime(s, 100, DataTime_Format, &p);
	return std::string(s);
}

std::string Tools::DateToStr(tm pDate)
{
	char s[100];
	strftime(s, 100, Data_Format, &pDate);
	return std::string(s);
}

time_t Tools::DateTimeStr2Stamp(const std::string& pDtStr)
{
	tm tm_ = Tools::DateTimeStr2TM(pDtStr);
	time_t t_ = mktime(&tm_); //已经减了8个时区
	return t_; //秒时间

}

tm Tools::DateTimeStr2TM(const std::string& pDtStr)
{
	tm tm_;
	int year, month, day, hour, minute, second;
	sscanf(pDtStr.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
	tm_.tm_year = year - 1900;
	tm_.tm_mon = month - 1;
	tm_.tm_mday = day;
	tm_.tm_hour = hour;
	tm_.tm_min = minute;
	tm_.tm_sec = second;
	tm_.tm_isdst = 0;
	return tm_;
}

tm Tools::DateStr2TM(const std::string& pDtStr)
{
	tm tm_;
	int year, month, day;
	sscanf(pDtStr.c_str(), "%d-%d-%d", &year, &month, &day);
	tm_.tm_year = year - 1900;
	tm_.tm_mon = month - 1;
	tm_.tm_mday = day;
	tm_.tm_hour = 0;
	tm_.tm_min = 0;
	tm_.tm_sec = 0;
	tm_.tm_isdst = 0;
	return tm_;
}

tm Tools::GetDate(const tm &pDateTime)
{
	tm tm_;
	tm_.tm_year = pDateTime.tm_year;
	tm_.tm_mon = pDateTime.tm_mon;
	tm_.tm_mday = pDateTime.tm_mday;
	tm_.tm_hour = 0;
	tm_.tm_min = 0;
	tm_.tm_sec = 0;
	tm_.tm_isdst = 0;
	return tm_;
}

tm Tools::AddDays(tm pDateTime, int nDays)
{
	return Tools::AddSecs(pDateTime, nDays * 60 * 60 * 24);
}

tm Tools::AddSecs(tm pDateTime, int nSecs)
{
	time_t t_ = mktime(&pDateTime); //已经减了8个时区
	t_ = t_ + nSecs;
	struct tm* ptm = localtime(&t_);
	return *ptm;
}


const std::string Tools::GetCurrentSystemTime()
{
	auto tt = Tools::GetCurrentTimeStamp();
	struct tm* ptm = localtime(&tt);
	char date[60] = { 0 };
	sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
		(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
		(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
	return std::string(date);
}

time_t Tools::GetCurrentTimeStamp()
{
	auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
	return tt;
}

long long Tools::GetCurrentMSTimeStamp()
{
	auto time_now = std::chrono::system_clock::now();
	auto duration_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_now.time_since_epoch());
	return duration_in_ms.count();
}

int Tools::Split(const char *pszSrc, char flag, std::vector<std::string> &vecDat)
{
	if (!pszSrc || !flag)
		return -1;

	std::string strContent, strTemp;
	strContent.assign(pszSrc);
	std::string::size_type nBeginPos = 0, nEndPos = 0;
	while (true)
	{
		nEndPos = strContent.find(flag, nBeginPos);
		if (nEndPos == std::string::npos)
		{
			// 把最后一个别落下
			strTemp = strContent.substr(nBeginPos);
			vecDat.push_back(strTemp);
			break;
		}
		strTemp = strContent.substr(nBeginPos, nEndPos - nBeginPos);
		nBeginPos = nEndPos + 1;
		vecDat.push_back(strTemp);
	}
	return vecDat.size();
}